package de.timoh.standardchatplugin;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.entity.Player;

/**
 * Diese Klasse stellt erweiterte Spielerobjekte zur Verfügung. Sie können genutzt
 * werden um zusätzliche Daten abzuspeichern.
 *
 * @version 0.1
 * @author Timo
 */
public class PlayerEx implements Serializable {
    public final static int RANK_ADMIN = 0;
    public final static int RANK_MOD = 1;
    public final static int RANK_STAMMSPIELER = 2;
    public final static int RANK_SPIELER = 3;
    public final static int RANK_GAST = 4;

    private static HashMap<String,PlayerEx> playerMap = new HashMap<String,PlayerEx>();


    private static String backupPath = "Backup/playerMapBackup.txt";
    //Sollte auch letzten login speichern!

    private int rank = 4;
    private String name = "";
    private double [] home = new double [3];
    private boolean isHomeSet = false;

    public static String [] toArrayKey(){
        String [] array = playerMap.keySet().toArray(new String[playerMap.size()]);
        java.util.Arrays.sort(array);
        return array;
    }

    public static PlayerEx [] toArrayValue(){
        PlayerEx [] array = playerMap.values().toArray(new PlayerEx[playerMap.size()]);
        return array;
    }

    public static String getAsString(){
        PlayerEx [] array = toArrayValue();
        String out = "";
        for(int i = 0; i < array.length;i++){
            if(array[i].isHomeSet){
                out = out + array[i].getName() + ":"+array[i].getRank()+":"+array[i].home[0]+","+array[i].home[1]+","+array[i].home[2]+";";
            }else{
                out = out + array[i].getName() + ":"+array[i].getRank()+":"+"false"+";";
            }
        }
        return out;
    }

    public static void saveAsTXT(){
        File file = new File(backupPath);
        String writeString = getAsString();
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            bw.write(writeString);
            bw.close();
            System.out.println("SCP: PlayerMap Backup war erfolgreich!");
        } catch (IOException ex) {
            System.err.println("Fehler: "+ex);
        }
    }

    public static void setPlayerMap(HashMap<String,PlayerEx> map){
        playerMap = map;
    }

    public static HashMap<String,PlayerEx> getPlayerMap(){
        return playerMap;
    }

    public static boolean isPlayerInMap(String key){
        return playerMap.containsKey(key);
    }

    public static PlayerEx getPlayer(String key){
        return playerMap.get(key);
    }

    public PlayerEx(String name){
        playerMap.put(name, this);
        this.name = name;
        PlayerHandler.save();
    }

    public void setRank(int rank,boolean save){
        if(this.rank == RANK_ADMIN && rank != RANK_ADMIN){
            PlayerHandler.delete(this.name,true);
        }else if(this.rank == RANK_MOD && rank != RANK_MOD){
            PlayerHandler.delete(this.name,false);
        }
        this.rank = rank;
        if(save){
            if(this.rank == RANK_ADMIN){
                PlayerHandler.save(this.name,true);
            }else if(this.rank == RANK_MOD){
                PlayerHandler.save(this.name,false);
            }   
        }
    }

    public void setDisplayName(Player player){
        if(rank == RANK_ADMIN){
            player.setDisplayName(ChatColor.GOLD +"[ADMIN]"+ player.getName()+ChatColor.WHITE);
        }else if(rank == RANK_MOD){
            player.setDisplayName(ChatColor.AQUA +"[MOD]"+ player.getName()+ChatColor.WHITE);
        }else if(rank == RANK_STAMMSPIELER){
            player.setDisplayName(ChatColor.LIGHT_PURPLE +"[Stammspieler]"+ player.getName()+ChatColor.WHITE);
        }else if(rank == RANK_SPIELER){
            player.setDisplayName(player.getName());
        }else if(rank == RANK_GAST){
            player.setDisplayName(ChatColor.GRAY+"[Gast]"+player.getName()+ChatColor.WHITE);
        }

    }

    public int getRank(){
        return this.rank;
    }

    public String getName(){
        return this.name;
    }

    public void setHome(Location loc){
        this.isHomeSet = true;
        this.home[0] = loc.getX();
        this.home[1] = loc.getY();
        this.home[2] = loc.getZ();
    }

    public Location getHome(Player player){
        return new Location(player.getWorld(),this.home[0],this.home[1],this.home[2]);
    }

    public boolean isHomeSet(){
        return isHomeSet;
    }

    @Override
    public String toString(){
        return this.name;
    }
}
